package com.example.personal.aiagent.chat.localservice;

import com.example.personal.aiagent.chat.commonservice.BaseChatService;
import com.example.personal.aiagent.chat.commonservice.ChatPool;
import com.example.personal.aiagent.chat.commonservice.StaticChatPool;
import com.example.personal.aiagent.common.util.UserUtil;
import com.example.personal.aiagent.xunfei.account.model.dto.AccountDTO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * 本地聊天服务
 */
@Slf4j
public class LocalChatAbility extends BaseChatService {
    /**
     * 模拟分析时间（毫秒）
     */
    private static final long MOCK_ANALYSE_MS = 3000;

    @Autowired
    private ChatPool chatPool;

    private String answer;

    public LocalChatAbility(AccountDTO accountDTO) {
        super(accountDTO);
    }

    @Override
    public void doStartChat() {
        log.info("local chat start");
    }

    @Override
    public void asyncInputChatContent(String content) {
        log.info("async input char content");
        String userName = UserUtil.getCurrentUser();
        answer = "not finished yet";
        ExecutorService executorService = new ThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());
        executorService.submit(() -> {
            try {
                Thread.sleep(MOCK_ANALYSE_MS);
                answer = content.replace('?', '!');
            } catch (InterruptedException ex) {
                log.error("asyncInputChatContent interrupted", ex);
            }
            super.setChatAnalyseFinish();
        });
    }

    @Override
    public String doGetChatResult() {
        return answer;
    }

    @Override
    public void doCloseChat() {
        log.info("local chat close");
    }

    @Override
    public void doForceCloseChat() {
        log.info("local chat force close");
    }
}
